<?php
/**
 * Create By: PhpStorm
 * User: yihua
 * File: GetUserFavorite.php
 * Date: 2017/2/6
 * Time: 21:50
 */

namespace app\api\model\page\user;

use app\api\model\dao\Goods;
use app\api\model\dao\NotePraise;
use app\api\model\dao\NoteReply;
use app\api\model\dao\UserAccount;
use app\api\model\dao\UserFavorite;
use app\api\model\page\note\GetNoteInfo;
use app\library\Error;
use app\library\PageBase;
use think\Log;
use think\Validate;

class GetUserFavorite extends PageBase
{
    private $objUserFavoriteModel;

    public function __construct()
    {
        parent::__construct();
        $this->objUserFavoriteModel = new UserFavorite();
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            //return $this->getRes(Error::ERR_NO_AUTH);
        }
        /*
        if (!isset($param['user_id']) || !isset($param['fav_type'])) {
            Log::error('param error: '.json_encode($param));
            return $this->getRes(Error::ERR_PARAM);
        }
        */
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }
        $favList = $this->objUserFavoriteModel->getFavorite($param['user_id'], $param['fav_type']);
        if (false === $favList) {
            Log::error('get favorite failed' . json_encode($favList));
            return $this->getRes(Error::ERR_SYS);
        }
        $data = [];
        //根据fav_id去查询相应的信息
        if ($param['fav_type'] == 1) {
            // 商品信息
            foreach ($favList as $val) {
                //var_dump($val->fav_id);
                $fav_id = $val->fav_id;
                $data[] = $this->getGoodsInfo($fav_id);
                //return ($data);
            }
        }
        if ($param['fav_type'] == 2) {
            //帖子信息
            foreach ($favList as $val) {
                $fav_id = $val->fav_id;
                $data[] = $this->getNoteInfo($fav_id);
            }
        }
        $this->data = $data;
        return $this->getRes();
    }

    public function getGoodsInfo($goods_id)
    {
        /*商品返回的价格，图片，名称，销量*/
        $tmp['fav_id'] = $goods_id;
        $objGoods = new Goods();
        $goodsInfo = $objGoods->getGoods($goods_id);
        if (false === $goodsInfo) {
            return false;
        } else {
            $tmp['shop_price'] = $goodsInfo[0]['shop_price'];
            $tmp['goods_img'] = $goodsInfo[0]['goods_img'];
            $tmp['goods_name'] = $goodsInfo[0]['goods_name'];
            $tmp['sale_count'] = $goodsInfo[0]['sale_count'];
            return $tmp;
        }


    }

    public function getNoteInfo($note_id)
    {
        /*帖子返回，title,描述，作者，作者头像,评论和点赞数。*/
        $data['fav_id'] = $note_id;
        //帖子信息
        $objNote = new GetNoteInfo();
        $noteInfo = $objNote->getNoteInfoFromDB($note_id);
        if (false === $noteInfo) {
            return false;
        } else {
            $tmp['title'] = $noteInfo['title'];
            $tmp['content'] = $noteInfo['content'];
            $tmp['creator'] = $noteInfo['creator'];
        }
        //$data['note_info'] = $noteInfo;
        $data['note_info'] = $tmp;
        //作者信息
        unset($tmp);
        $objUserAccount = new UserAccount();
        $user_account = $objUserAccount->getUserAccount(['user_id' => $noteInfo['creator']]);
        if (false === $user_account) {
            return false;
        } else {
            $tmp['nick_name'] = $user_account[0]['nick_name'];
            $tmp['avatar'] = $user_account[0]['avatar'];
            //$tmp['title'] = $noteInfo['title'];
        }
        $data['creator_count'] = $tmp;
        //回复数量
        $objReply = new NoteReply();
        $noteReplyCnt = $objReply->getNoteReplyCountByNoteid($note_id);
        $data['reply_info'] = $noteReplyCnt;
        //点赞数量
        $objNotePraise = new NotePraise();
        //$notePraiseCnt = $objNotePraise->getNoteReplyCountByNoteid($note_id);
        $notePraiseCnt = $objNotePraise->getNotePraiseCountByNoteid($note_id);
        $data['praise_count'] = $notePraiseCnt;

        return $data;

    }


    public function checkParam($param)
    {
        $rule = [
            'user_id' => 'require',
            'fav_type' => 'require|in:1,2',
        ];
        $msg = [
            'user_id.require' => 'user_id必须',
            'fav_type.require' => 'fav_type必须',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }
}
